package com.cmcc.aregister.ui.activity;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.model.NoticeModel;
import com.cmcc.aregister.cs.util.ImageCacheUtil;
import com.cmcc.aregister.cs.util.ImageCacheUtil.ImageCallback;
import com.cmcc.aregister.cs.util.Utils;

public class NoticeDetailActivity extends ParentActivity implements
    OnClickListener
{

    private NoticeModel notice;
    private String root;

    private TextView titleView, noticeTitle, noticeContent, noticeTime;
    private ImageView noticeImageView;
    private int jump_type = Arguments.NOTICE_JUMP_TYPE_NOTIFICATION;// 默认是从通知处来到该界面，返回到首页

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        init(R.layout.p_noticedetail);
        mHttpManager.setCxt(getParent());
    }

    private void initRoot()
    {
        if (hasSdcard())
        {
            root = Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "yygh";
            ;
        }
        else
        {
            root = getApplicationContext().getCacheDir().getAbsolutePath();
        }
        File dir = new File(root);
        if (!dir.exists())
        {
            dir.mkdirs();
        }

    }

    public void onBackPressed()
    {
        // super.onBackPressed();
        gotoListActivity();
    };

    private void setData()
    {
        if (notice != null)
        {
            if (Utils.isNotEmpty(notice.link)
                && !"http://".equalsIgnoreCase(notice.link))
            {
                // top_right_btn.setVisibility(View.VISIBLE);
            }
            else
            {
                // top_right_btn.setVisibility(View.GONE);
            }
            initRoot();
            noticeTitle.setText(notice.title);
            noticeTime.setText(getDateStr(notice.create_time,
                "yyyy年MM月dd日 HH:mm:ss"));
            noticeContent.setText("\u3000\u3000" + notice.content);
            if (Utils.isNotEmpty(notice.image))
            {
                noticeImageView.setVisibility(View.VISIBLE);
                ImageCacheUtil cacheUtil = new ImageCacheUtil();
                cacheUtil.setPath(root);
                cacheUtil.setSaveFile(true);
                cacheUtil.getImage(notice.image, new ImageCallback() {

                    @Override
                    public void onImageLoaded(Bitmap bitmap)
                    {
                        if (bitmap != null)
                        {
                            noticeImageView.setImageBitmap(bitmap);
                        }
                    }
                });
            }
            else
            {
                noticeImageView.setVisibility(View.GONE);
            }
        }
    }

    private String getDateStr(String str, String format)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
            Locale.CHINA);
        String date = "";
        try
        {
            Date d = sdf.parse(str);
            date = formatDate2String(d.getTime(), format);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将日期Time转换为日期字符串
     * @param time
     * @param format 格式 MM月dd日 HH:mm
     * @return
     */
    private String formatDate2String(long time, String format)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    @Override
    protected void setupData()
    {
        notice = (NoticeModel) getIntent().getSerializableExtra(
            Arguments.NOTICE_MODEL);
        jump_type = getIntent().getIntExtra(Arguments.NOTICE_JUMP_TYPE,
            Arguments.NOTICE_JUMP_TYPE_NOTIFICATION);
    }

    @Override
    protected void setupView()
    {
        // top_left_image = (Button) findViewById(R.id.top_left_image);
        // top_left_btn = (Button) findViewById(R.id.top_left_btn);
        // top_left_btn.setOnClickListener(this);
        // top_right_btn = (Button) findViewById(R.id.top_right_btn);
        // top_right_btn.setOnClickListener(this);
        ((ImageView) findViewById(R.id.title))
            .setBackgroundResource(R.drawable.notice_detail);
        noticeTitle = (TextView) findViewById(R.id.notice_title);
        noticeContent = (TextView) findViewById(R.id.notice_content);
        noticeTime = (TextView) findViewById(R.id.notice_time);
        noticeImageView = (ImageView) findViewById(R.id.notice_image);

        // top_left_image.setVisibility(View.GONE);
        // top_left_btn.setVisibility(View.VISIBLE);
        // top_left_btn.setText(R.string.back);
        // top_right_btn.setText(R.string.detail);

        setData();
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.top_left_btn:
            gotoListActivity();
            break;
        case R.id.top_right_btn:
            Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(notice.link));
            // intent.setClassName("com.android.browser",
            // "com.android.browser.BrowserActivity");
            startActivity(intent);
            break;
        default:
            break;
        }
    }

    private void gotoListActivity()
    {
        Intent intent = new Intent(this, NoticeActivity.class);
        intent.putExtra(Arguments.NOTICE_JUMP_TYPE,
            Arguments.NOTICE_JUMP_TYPE_NOTIFICATION);
        intent.putExtra(Arguments.NOTICE_JUMP_TYPE, jump_type);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    private boolean hasSdcard()
    {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
